package com.iamteer.contact.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.iamteer.contact.dao.ContactDao;
import com.iamteer.contact.entity.Contact;
import com.iamteer.contact.util.JdbcUtil;

public class ContactDaoMySQLImpl implements ContactDao {

	@Override
	public void addContact(Contact contact) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			//1 获取连接对象
			conn = JdbcUtil.getConnection();
			
			//2 准备 sql
			String sql = "INSERT INTO contact(id,name,gender,age,phone,email,qq) VALUES(?,?,?,?,?,?,?)";
			
			//3 创建 PreparedStatement，预编译
			stmt = conn.prepareStatement(sql);
			
			//4 设置参数
			String id = UUID.randomUUID().toString().replaceAll("-", "");
			stmt.setString(1, id);
			stmt.setString(2, contact.getName());
			stmt.setString(3, contact.getGender());
			stmt.setInt(4, contact.getAge());
			stmt.setString(5, contact.getPhone());
			stmt.setString(6, contact.getEmail());
			stmt.setString(7, contact.getQq());
			
			//5 发送 sql，执行 sql
			stmt.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.close(conn, stmt);
		}
	}

	@Override
	public void updateContact(Contact contact) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			//1 获取连接对象
			conn = JdbcUtil.getConnection();
			
			//2 准备 sql
			String sql = "UPDATE contact SET name=?,gender=?,age=?,phone=?,email=?,qq=? WHERE id=?";
			
			//3 创建 PreparedStatement，预编译
			stmt = conn.prepareStatement(sql);
			
			//4 设置参数
			String id = contact.getId();
			stmt.setString(1, contact.getName());
			stmt.setString(2, contact.getGender());
			stmt.setInt(3, contact.getAge());
			stmt.setString(4, contact.getPhone());
			stmt.setString(5, contact.getEmail());
			stmt.setString(6, contact.getQq());
			stmt.setString(7, id);
			
			//5 发送 sql，执行 sql
			stmt.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.close(conn, stmt);
		}
	}

	@Override
	public void deleteContact(String id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			//1 获取连接对象
			conn = JdbcUtil.getConnection();
			
			//2 准备 sql
			String sql = "DELETE FROM contact where id=?";
			
			//3 创建 PreparedStatement，预编译
			stmt = conn.prepareStatement(sql);
			
			//4 设置参数
			stmt.setString(1, id);
			
			//5 发送 sql，执行 sql
			stmt.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.close(conn, stmt);
		}
	}

	@Override
	public List<Contact> findAll() {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			//1 获取连接对象
			conn = JdbcUtil.getConnection();
			
			//2 准备 sql
			String sql = "SELECT * FROM contact";
			
			//3 创建 PreparedStatement，预编译
			stmt = conn.prepareStatement(sql);
			
			//4 发送 sql，执行 sql，返回结果
			ResultSet rs = stmt.executeQuery();
			
			//5 整理结果返回
			List<Contact> list = new ArrayList<Contact>();
			while (rs.next()) {
				Contact c = new Contact();
				c.setId(rs.getString("id"));
				c.setName(rs.getString("name"));
				c.setGender(rs.getString("gender"));
				c.setAge(rs.getInt("age"));
				c.setPhone(rs.getString("phone"));
				c.setEmail(rs.getString("email"));
				c.setQq(rs.getString("qq"));
				list.add(c);
			}
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.close(conn, stmt);
		}
	}

	@Override
	public Contact findById(String id) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			//1 获取连接对象
			conn = JdbcUtil.getConnection();
			
			//2 准备 sql
			String sql = "SELECT * FROM contact WHERE id=?";
			
			//3 创建 PreparedStatement，预编译
			stmt = conn.prepareStatement(sql);
			//4 设置参数
			stmt.setString(1, id);
			
			//5 发送 sql，执行 sql，返回结果
			ResultSet rs = stmt.executeQuery();
			
			//6 整理结果返回
			Contact contact = null;
			if (rs.next()) {
				contact = new Contact();
				contact.setId(rs.getString("id"));
				contact.setName(rs.getString("name"));
				contact.setGender(rs.getString("gender"));
				contact.setAge(rs.getInt("age"));
				contact.setPhone(rs.getString("phone"));
				contact.setEmail(rs.getString("email"));
				contact.setQq(rs.getString("qq"));
			}

			return contact;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.close(conn, stmt);
		}
	}

	@Override
	public boolean exists(String name) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			//1 获取连接对象
			conn = JdbcUtil.getConnection();
			
			//2 准备 sql
			String sql = "SELECT * FROM contact WHERE name=?";
			
			//3 创建 PreparedStatement，预编译
			stmt = conn.prepareStatement(sql);
			//4 设置参数
			stmt.setString(1, name);
			
			//5 发送 sql，执行 sql，返回结果
			ResultSet rs = stmt.executeQuery();
			
			//6 整理结果返回
			return rs.next();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			JdbcUtil.close(conn, stmt);
		}
	}

}
